import { Controller } from "../../mvc/core/Controller";
import { ILoginBackRewardController } from "./interfaces/ILoginBackRewardController";
import { IView } from "../../mvc/interfaces/IView";
import { LoginBackRewardModule } from "../../../modules/LoginBackRewardModule";
import { LoginBackRewardIcon } from "../view/loginBackReward/LoginBackRewardIcon";
import { ResFileConst } from "../resource/ResFileConst";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { LoginBackRewardData } from "../view/loginBackReward/LoginBackRewardData";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { ILoginBackRewardModule } from "../../../modules/interfaces/ILoginBackRewardModule";
type int = number;
//class LoginBackRewardController
    
    export  class LoginBackRewardController extends Controller implements ILoginBackRewardController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._loginBackReward == null) 
            {
                this._loginBackReward = new LoginBackRewardModule();
            }
            return this._loginBackReward;
        }

        public get loginBackIcon(): LoginBackRewardIcon
        {
            if (this._loginBackIcon == null) 
            {
                this._loginBackIcon = new LoginBackRewardIcon(ResFileConst.LoginBackRewardBtn);
                this._loginBackIcon.updateIconInfo();
            }
            return this._loginBackIcon;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.ActiveTwoYearLoginGiveBack, this.onTwoYearLoginGiveBackHandler);
            Dispatcher.addEventListener(EventName.LoginBackRewardOpen, this.onLoginBackRewardOpenHandler);
            Dispatcher.addEventListener(EventName.LoginBackToGetRewardInfo, this.onLoginBackToGetRewardInfoHandler);
            Dispatcher.addEventListener(EventName.LoginBackToGetRewardInfoSuccess, this.onLoginBackToGetRewardInfoSuccessHandler);
            Dispatcher.addEventListener(EventName.LoginBackLuckyDrawReward, this.onLoginBackLuckyDrawRewardHandler);
            Dispatcher.addEventListener(EventName.LoginBackLuckyDrawRewardSuccess, this.onLoginBackLuckyDrawRewardSuccessHandler);
            Dispatcher.addEventListener(EventName.LoginBackToGetTotalReward, this.onLoginBackToGetTotalRewardHandler);
            Dispatcher.addEventListener(EventName.LoginBackToGetTotalRewardSuccess, this.onLoginBackToGetTotalRewardSuccessHandler);
            return;
        }

        /* internal  */onTwoYearLoginGiveBackHandler(arg1: SActiveToClient): void
        {
            var loc1=cache.active.isActiveInEffect(arg1.code);
            if (loc1 && cache.role.entityInfo.level >= 35) 
            {
                this.loginBackIcon.show();
            }
            return;
        }

        /* internal  */onLoginBackRewardOpenHandler(arg1: DataEvent): void
        {
            if (cache.role.entityInfo.level < 35) 
            {
                return;
            }
            if (view.isHide) 
            {
                view.show();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onLoginBackToGetRewardInfoHandler(arg1: DataEvent): void
        {
            GameProxy.daily.getLoginBackRewardInfo();
            return;
        }

        /* internal  */onLoginBackToGetRewardInfoSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.rewardInfo as Array<any>;
            var loc3=loc1.luckyDrawTimes as int;
            var loc4=loc1.luckyDrawInfo as Array<any>;
            var loc5=loc1.continueDays as int;
            var loc6=loc1.totalDays as int;
            LoginBackRewardData.instance.continueDays = loc5;
            LoginBackRewardData.instance.totalDays = loc6;
            if (this._loginBackReward && !this._loginBackReward.isHide) 
            {
                this._loginBackReward.updateDayRewardInfo(loc4);
                this._loginBackReward.updateAllRewardInfo(loc2);
                this._loginBackReward.updateLuckyDrawTimes(loc3, loc5);
                this._loginBackReward.updateTotalDay(loc5, loc6);
            }
            return;
        }

        /* internal  */onLoginBackLuckyDrawRewardHandler(arg1: DataEvent): void
        {
            if (this._timer == null) 
            {
                this._timer = new SecTimer();
                this._timer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
            }
            this._timer.reset();
            this._timer.repeatCount = 1;
            this._timer.start();
            this._pos = arg1.data as int;
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: SecTimer): void
        {
            arg1.stop();
            arg1.isDelete = false;
            GameProxy.daily.luckyDraw(this._pos);
            return;
        }

        /* internal  */onLoginBackLuckyDrawRewardSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.luckyDrawInfo as Array<any>;
            var loc3=loc1.luckyDrawTimes as int;
            if (this._loginBackReward && !this._loginBackReward.isHide) 
            {
                this._loginBackReward.updateDayRewardInfo(loc2);
                this._loginBackReward.updateLuckyDrawTimes(loc3, LoginBackRewardData.instance.continueDays);
            }
            return;
        }

        /* internal  */onLoginBackToGetTotalRewardHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.daily.getLoginReward(loc1);
            return;
        }

        /* internal  */onLoginBackToGetTotalRewardSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            if (this._loginBackReward && !this._loginBackReward.isHide) 
            {
                this._loginBackReward.updateAllRewardInfo(loc1);
            }
            return;
        }

        private /* var */_loginBackReward: ILoginBackRewardModule;

        private /* var */_loginBackIcon: LoginBackRewardIcon;

        private /* var */_timer: SecTimer;

        private /* var */_pos: int;
    }
